#include "occipool.hpp"
#include <iostream>


using namespace com_oocl_monitor::system;


OcciPool::OcciPool(const string & constr, const string & usr, const string & pwd, int maxCon, int minCon, int incrCon)
	: _constr(constr), _usr(usr), _pwd(pwd), _maxCon(maxCon), _minCon(minCon), _incrCon(incrCon) {
		_occiEnv = OcciEnv::getInstance();
		_pool = _occiEnv->Env()->createStatelessConnectionPool(usr, pwd, constr, maxCon, minCon, incrCon, StatelessConnectionPool::HOMOGENEOUS);
		_pool->setTimeOut(120);
		//    _pool=_occiEnv->Env()->createConnectionPool(usr,pwd,constr,minCon,maxCon,incrCon);
		CreateSemaphore(NULL, _maxCon, _maxCon, NULL);
}

OcciPool::~OcciPool() {
	_occiEnv->Env()->terminateStatelessConnectionPool(_pool);
	CloseHandle(_sem);
	//    _occiEnv->Env()->terminateConnectionPool(_pool);
}

Connection *OcciPool::getConnection() {
	Connection *rs = nullptr;
	WaitForSingleObject(_sem, INFINITE);
	//    rs=_pool->createConnection(_usr,_pwd);
	rs=_pool->getConnection();
	return rs;
}

void OcciPool::releaseConnection(Connection *con) {

	//    _pool->terminateConnection(con);
	if(con!=nullptr)
		_pool->releaseConnection(con);
	ReleaseSemaphore(_sem, 1, NULL);
}
